package com.poetize.plus.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.poetize.plus.utils.LoginUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 *
 * mybatis-plus统一字段自动插入
 *
 * @author zuosy
 * @created 2024/3/28 19:18
 */
@Component
public class MetaHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createBy", LoginUtils.getLoginUserName(), metaObject);
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateBy", LoginUtils.getLoginUserName(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy", LoginUtils.getLoginUserName(), metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }
}
